package cn.tedu.submarine;
import java.util.Random;
import javax.swing.ImageIcon;
import java.awt.Graphics;

public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    int state = LIVE;
    int width,height,x,y,speed;
    Random rand = new Random();
    public SeaObject(int width,int height) {
        this.width = width;
        this.height = height;
        x = -width;
        y = rand.nextInt(World.HEIGHT-height-150+1) + 150;
        speed = rand.nextInt(3) + 1;
    }
    public SeaObject(int width,int height,int x,int y,int speed) {
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    public abstract void move();
    public abstract ImageIcon getImage();
    public boolean isLive() {
        return state == LIVE;
    }
    public boolean isDead() {
        return state == DEAD;
    }
    public void paintImage(Graphics g) {
        if(this.isLive()) {
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }
    public boolean isOutOfBounds() {
        return this.x >= World.WIDTH;
    }
   public boolean isHit(SeaObject other) {
        //假设this指潜艇，水雷，other指炸弹，战舰
       int x1 = this.x-other.width;
       int x2 = this.x+this.width;
       int y1 = this.y-other.height;
       int y2 = this.y+this.height;
       int x = other.x;
       int y = other.y;
       return  x > x1 && x < x2
               &&
               y > y1 && y < y2;
   }
   public void goDead() {
        state = DEAD;
   }
}
